package com.lichuang.dedao.listenBooks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.listenBooks.entity.Category;
import com.lichuang.dedao.listenBooks.entity.ListenBooks;
import com.lichuang.dedao.listenBooks.mapper.CategoryMapper;
import com.lichuang.dedao.listenBooks.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lichuang.dedao.listenBooks.service.IListenBooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author kangkang
 * @since 2019-07-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private IListenBooksService listenBooksService;

    @Override
    public List<Category> selectCategory(Integer pid) {
        return baseMapper.selectCategory(pid);
    }

    @Override
    public Category selectCategoryById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Category> selectCategoriesByPid(Integer pid) {
        Category categoryParam = new Category();
        categoryParam.setPid(pid);
        QueryWrapper queryWrapper = new QueryWrapper(categoryParam);
        List<Category> categories = baseMapper.selectList(queryWrapper);
        return categories;
    }

    @Override
    public List<Category> selectCategoryByListenBook() {
        /**
         * 通过书籍的List集合，查询出二级分类id
         * 通过二级分类id获取此分类
         * 通过此二级分类获取到pid
         * 通过pid获取到一级分类
         *
         */
        // 查询所有听书
        List<ListenBooks> listenBooks = listenBooksService.selectLisenBooks();
        // 二级分类的集合
        List<Category> categoriesTwo = new LinkedList<>();
        // 遍历书籍，将得到的二级分类保存到集合中
        for (ListenBooks listenBook : listenBooks) {
            // 二级分类id
            Integer categoryTwoId = listenBook.getCategoryId();
            // 二级分类
            Category categoryTwo = selectCategoryById(categoryTwoId);
            categoriesTwo.add(categoryTwo);
        }
        // 一级分类的集合
        List<Category> categoriesOne = new LinkedList<>();
        // 遍历二级分类，得到对应的一级分类，判断一级分类集合中是否存在相同的一级分类，如果有则遍历一级分类中的二级分类集合，判断是否存在相同的二级分类，如果不存在则添加到一级分类集合中
        for (Category categoryTwo : categoriesTwo) {
            Integer categoryTwoPid = categoryTwo.getPid();
            Category categoryOne = selectCategoryById(categoryTwoPid);
            boolean isNew = true;
            for (Category categoryList : categoriesOne) {
                if (categoryList.getId() == categoryOne.getId()){
                    isNew = false;
                    List<Category> childCategory = categoryList.getCategories();
                    boolean repet = false;
                    for (Category category : childCategory) {
                        if (category.getId() == categoryTwo.getId()){
                            repet = true;
                            break;
                        }
                    }
                    if (!repet){
                        childCategory.add(categoryTwo);
                    }
                }
            }
            if (isNew){
                categoryOne.getCategories().add(categoryTwo);
                categoriesOne.add(categoryOne);
            }
        }
        return categoriesOne;
    }

}

